package com.ahnu.tongue.controller;

import com.ahnu.tongue.annotation.OperationLog;
import com.ahnu.tongue.annotation.RequireLogin;
import com.ahnu.tongue.common.Result;
import com.ahnu.tongue.dto.TongueAnalysisRequest;
import com.ahnu.tongue.dto.TongueAnalysisResponse;
import com.ahnu.tongue.service.TongueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "舌象诊断接口")
@RestController
@RequestMapping("/tongue")
@RequireLogin
@RequiredArgsConstructor
@Validated
public class TongueController {

    private final TongueService tongueService;

    @ApiOperation(value = "上传舌象图片并分析", notes = "上传舌象图片进行AI分析，返回分析结果")
    @PostMapping("/upload")
    @OperationLog(operationType = "UPLOAD", description = "上传舌象图片并分析")
    public Result<TongueAnalysisResponse> uploadAndAnalyze(
            @ApiParam(value = "舌象图片", required = true) 
            @NotNull(message = "图片不能为空")
            @RequestParam("image") MultipartFile image,
            @ApiParam(value = "备注信息") 
            @RequestParam(value = "remark", required = false) String remark,
            HttpServletRequest request) {
        
        Long userId = (Long) request.getAttribute("userId");
        TongueAnalysisRequest analysisRequest = new TongueAnalysisRequest();
        analysisRequest.setImage(image);
        analysisRequest.setUserId(userId);
        analysisRequest.setRemark(remark);
        
        return Result.success(tongueService.analyzeImage(analysisRequest));
    }

    @ApiOperation(value = "获取诊断报告详情", notes = "根据记录ID获取舌象诊断报告详细信息")
    @GetMapping("/{recordId}")
    @OperationLog(operationType = "QUERY", description = "查看诊断报告详情")
    public Result<TongueAnalysisResponse> getAnalysisResult(
            @ApiParam(value = "记录ID", required = true) 
            @PathVariable @NotNull(message = "记录ID不能为空") Long recordId,
            HttpServletRequest request) {
        
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(tongueService.getAnalysisResult(recordId, userId));
    }

    @ApiOperation(value = "获取诊断历史记录", notes = "分页获取用户的舌象诊断历史记录")
    @GetMapping("/history")
    @OperationLog(operationType = "QUERY", description = "查看诊断历史记录")
    public Result<List<TongueAnalysisResponse>> getHistoryRecords(
            @ApiParam(value = "页码", example = "1") 
            @RequestParam(defaultValue = "1") @Min(value = 1, message = "页码必须大于0") int page,
            @ApiParam(value = "每页大小", example = "10") 
            @RequestParam(defaultValue = "10") @Min(value = 1, message = "每页大小必须大于0") int size,
            HttpServletRequest request) {
        
        Long userId = (Long) request.getAttribute("userId");
        return Result.success(tongueService.getHistoryRecords(userId, page, size));
    }

    @ApiOperation(value = "删除诊断记录", notes = "删除指定的舌象诊断记录")
    @DeleteMapping("/{recordId}")
    @OperationLog(operationType = "DELETE", description = "删除诊断记录")
    public Result<Void> deleteRecord(
            @ApiParam(value = "记录ID", required = true) 
            @PathVariable @NotNull(message = "记录ID不能为空") Long recordId,
            HttpServletRequest request) {
        
        Long userId = (Long) request.getAttribute("userId");
        tongueService.deleteRecord(recordId, userId);
        return Result.success();
    }
} 